Update and Add Data

Update Record Data

Description
This example demonstrates how to write a reusable function that programmatically edits/updates an existing database record. Specifically, this example updates a Products record. The reusable function can be called from any page. Although the function is trivial, the purpose is to demonstrate how to programmatically update database records. The example code could be easily modified to update multiple records returned by a filtered query, etc.
Variables
Table Name
Select the name of the database table.
Applies to
Page class
Code
 
''' 
''' Updates a record in the table.
''' 
Public Shared Sub UpdateRecord()

    ' Get an existing record with ID 1.

    Dim rec As New ${${Table Name}RecordClassName}   

    ' Set second parameter to True in GetRecord() to retrieve an updatable record.
    rec = ${${Table Name}ClassName}.GetRecord("1", True )

    ' Update the fields of the record as shown below.
    ' EmployeesRecord.LastName = "LastName"
    ' EmployeesRecord.FirstName = "FirstName"

    ' Save the update.
    rec.Save() 
    
    ' Example shown below shows the transaction handling. 
    ' Note that you need to start a transaction before calling
    ' UpdateRecord() function.
    
    ' Try
    '   DbUtils.StartTransaction()
    '   UpdateRecord()
    '   DbUtils.CommitTransaction()
    '   Me.DataChanged=True

    ' Catch ex As Exception
    '   DbUtils.RollBackTransaction()
    '   Utils.MiscUtils.RegisterJScriptAlert(Me, "UNIQUE_SCRIPTKEY", ex.Message)

    ' Finally
    '    DbUtils.EndTransaction()
    ' End Try  
    
End Sub
     

Terms of Service Privacy Statement